<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script src="./../jquery/jquery 3.6.4.js"></script>
  <script>


    // async function fn1() {
    //   return 1; //  return Promise.resolve(1)
    // }

    // console.log(fn1())  // Promise实例对象，

    /*   async function fn1() {
        let n = Math.round(Math.random() * 10);
        if (n % 2 == 0) {
          return Promise.resolve(n)
        } else {
          return Promise.reject(n)
        }
      }
  
      // console.log(fn1());
      fn1()
        .then(res => console.log(res))
        .catch(err => console.log(err)) */
    /* 
        async function fn1() {
          return new Promise((resolve, reject) => {
            setTimeout(function () {
              let n = Math.round(Math.random() * 10);
              if (n % 2 == 0) {
                return resolve(n)
              }
              else {
                return reject(n)
              }
            }, 2000)
          });
    
    
        }
    
        async function fn2() {
          return new Promise((resolve, reject) => {
            setTimeout(function () {
              let n = Math.round(Math.random() * 10);
              if (n % 5 == 0) {
                return resolve('yes' + n)
              }
              else {
                return reject("no-" + n)
              }
            }, 1000)
          });
        }
        //  await 必须在async函数里面使用
    
        async function fn3() {
    
          let a = await fn1()
          console.log(a); // 2,返回Primise对象兑现后的值，不再是Promise对象了。
    
          let b = await fn2();
          console.log(b);
    
          // let a =  fn1()
          // a.then(res => console.log(res))
          //   .catch(err => console.log(err));
          // let b = fn2();
          // b.then(res => console.log(res))
          //   .catch(err => console.log(err));
    
        }
    
        fn3(); */

    const fn1 = () => {
      return new Promise((resolve, reject) => {
        $.ajax({
          url: "/getAllDoctors", type: 'get', success(res) {
            if (res.code === 200) {
              resolve(res.data)
            } else {
              reject('失败1')
            }
          }
        })
      });
    }
    const fn2 = () => {
      return new Promise((resolve, reject) => {
        $.ajax({
          url: "/getJobs", type: 'get', success(res) {
            if (res.code === 200) {
              resolve(res.data)
            } else {
              reject('失败2')
            }
          }
        })
      })
    };

    const fn3 = async function () {
      let d1 = await fn1(); // doctors的数据
      let d2 = await fn2(); // jobs的数据
      // console.log('合并数据', d1.concat(d2));
      // 加了await后，能确保再执行forEach时，d1,d2都已经有数据。
      d1.forEach(el => {
        d2.forEach(item => {
          if (el.jid === item.jid) {
            el.jname = item.jname;
            el.jdes = item.jdes;
          }
        })
      })
      console.log(d1);
    }

    fn3()

    /*   function fn4() {
        fn1().then(res => {
          let d1 = res;
          fn2().then(r2 => {
            let d2 = r2;
            d1.forEach(el => {
              d2.forEach(item => {
                if (el.jid === item.jid) {
                  el.jname = item.jname;
                  el.jdes = item.jdes;
                }
              })
            })
            console.log(d1);
          })
        })
      }
      fn4(); */
    /*
        Promise.all([fn1(), fn2()]).then(res => {
          console.log(res)
          let d1 = res[0]
          let d2 = res[1];
          d1.forEach(el => {
            d2.forEach(item => {
              if (el.jid === item.jid) {
                el.jname = item.jname;
                el.jdes = item.jdes;
              }
            })
          })
          console.log(d1);
    
        }) */
  </script>
</body>

</html>